//
//  BHComInputTextCell.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/6/24.
//

import UIKit

class BHComInputTextCell: UITableViewCell {

    var textFiledBegainEditingBlock:(() -> Void)?
    var textFiledDidEndEditingBlock:((String) -> Void)?

    
    @IBOutlet weak var name_label: UILabel!
    
    @IBOutlet weak var text_filed: UITextField!
    
    @IBOutlet weak var line: UILabel!
    
    
    var model: BHFunCreateRowModel? {
        didSet {
            if model == nil {
                return
            }
            self.name_label.text = model!.name
            if model?.canTextEdite == true {
                self.text_filed.placeholder = "请输入\(model!.name)"
            }else {
                self.text_filed.placeholder = "请选择\(model!.name)"
            }
            self.text_filed.keyboardType = model!.keyBoardType
        }
    }
    
    var canTextCanEdite: Bool = true
    
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.text_filed.delegate = self
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}


extension BHComInputTextCell: UITextFieldDelegate {
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        self.textFiledBegainEditingBlock?()
        if self.model != nil {
            return self.model!.canTextEdite
        }else {
            return self.canTextCanEdite
        }
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        self.textFiledDidEndEditingBlock?(textField.text ?? "")
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return false
    }
}
